package com.csc531.adapters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.csc531.DataTypes.Task;
import com.csc531.views.TaskView;

/**
 * @author Javier Figueroa
 *
 */
public class TaskListAdapter extends CustomListAdapter
{
	ArrayList<Task> _sortedTasks = new ArrayList<Task>();

	public TaskListAdapter(Context context) 
	{
		super(context);
	}

	public void addTask(Task newTask)
	{
		_tasks.add(newTask);
	}

	public void setExpandedPropertyOnList() 
	{
		int sizeOfList = _tasks.size();

		int i = 0;
		while( i < sizeOfList ) {
			_mExpanded.add(false);
			i++;
		}
	}

	public void sortTasksByDate() throws ParseException
	{
		String strDateFormat = "MM-dd-yy HH:mm";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strDateFormat);
		Date currentDate = new Date();

		addTasksDueToSortedList(currentDate, simpleDateFormat);
		sortTasksDue(simpleDateFormat);
		addTasksPastDueToSortedList();

		_tasks = _sortedTasks;
	} 


	private void addTasksDueToSortedList(Date currentDate, SimpleDateFormat simpleDateFormat) throws ParseException
	{
		int listSize = _tasks.size();

		for(int i = 0; i<listSize ; i++)
		{
			Task task = _tasks.get(i);
			Date date = simpleDateFormat.parse(task.getDate());

			if(currentDate.compareTo(date) < 0)
			{
				task.isComingUp(true);
				_sortedTasks.add(0, task);
			}
		}
	}

	private void sortTasksDue(SimpleDateFormat simpleDateFormat) throws ParseException
	{
		int sortedSize = _sortedTasks.size();

		for(int i = 0 ; i < sortedSize ; i++)
		{
			int j = (i+1) % sortedSize;

			Task task = _sortedTasks.get(i);
			Date date = simpleDateFormat.parse(task.getDate());

			Task task2 = _sortedTasks.get(j);
			Date date2 = simpleDateFormat.parse(task2.getDate());

			if(date.compareTo(date2) < 0)
			{
				_sortedTasks.set(i, task2);
				_sortedTasks.set(j, task);
			}
		}
	}

	private void addTasksPastDueToSortedList()
	{
		int listSize = _tasks.size();

		for(int i = 0; i<listSize ; i++)
		{
			Task task = _tasks.get(i);

			if(_sortedTasks.contains(task) == false)
			{
				_sortedTasks.add(task);
			}
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) 
	{
		View sv = new View(mContext);

		Task task = _tasks.get(position);
		if (convertView == null) 
		{
			sv = new TaskView(mContext, task, _mExpanded.get(position));
		} 
		else 
		{
			sv = (TaskView)convertView;
			((TaskView) sv).setTitle(task);
			((TaskView) sv).setDialogue(task);
			((TaskView) sv).setExpanded(_mExpanded.get(position));
		}

		colorTitle(((TaskView)sv).getTitle(), task);

		return sv;
	}

	public void toggle(int position) 
	{
		_mExpanded.set(position,!_mExpanded.get(position));
		notifyDataSetChanged();
	}

	public void colorTitle(TextView view, Task task)
	{
		if(task.isComingUp())
		{
			view.setTextColor(Color.parseColor("#ffff6600"));
		}
		else
		{
			view.setTextColor(Color.parseColor("#ff99ff99"));
		}
	}

}
